﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using System.Web.Security;
using Website.Models;
using BusinessLogic;
using DataModels;

namespace Website.Controllers
{
    public class AccountController : Controller
    {

        private SecurityBusinessLogic _security = new SecurityBusinessLogic();

        //
        // GET: /Account/LogOn

        public ActionResult LogOn()
        {
            return View();
        }

        //
        // POST: /Account/LogOn

        [HttpPost]
        public ActionResult LogOn(LogOnModel model, string returnUrl)
        {
            if (ModelState.IsValid)
            {
                Person person = _security.Login(model.UserName, model.Password);

                if (person != null)
                {
                    FormsAuthentication.SetAuthCookie(person.FullName, model.RememberMe);
                    if (Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1 && returnUrl.StartsWith("/")
                        && !returnUrl.StartsWith("//") && !returnUrl.StartsWith("/\\"))
                    {
                        return Redirect(returnUrl);
                    }
                    else
                    {
                        return RedirectToAction("Index", "Home");
                    }
                }
                else
                {
                    ModelState.AddModelError("", "用户名或者密码不正确");
                }
            }

            // If we got this far, something failed, redisplay form
            return View(model);
        }

        //
        // GET: /Account/LogOff

        public ActionResult LogOff()
        {
            FormsAuthentication.SignOut();

            return RedirectToAction("Index", "Home");
        }

        //
        // GET: /Account/Register

        public ActionResult Register()
        {
            return View();
        }

        //
        // POST: /Account/Register

        [HttpPost]
        public ActionResult Register(RegisterModel model)
        {
            if (ModelState.IsValid)
            {
                // Attempt to register the user
                Security security = new Security();
                security.Username = model.UserName;
                security.Password = model.Password;
                if (model.IsIndividual)
                {
                    JobSeeker seeker = new JobSeeker();
                    seeker.RegisterTime = DateTime.Now;
                    seeker.Mobile = string.Empty;
                    seeker.FullName = model.FullName;
                    seeker.Email = model.Email;
                    seeker.Education = "Master";

                    security.Person = seeker;
                }
                else
                {
                    Company company = new Company();
                    company.FullName = model.FullName;
                    company.RegisterTime = DateTime.Now;
                    company.Mobile = string.Empty;
                    company.Email = model.Email;
                    company.Address = "company Address";

                    security.Person = company;
                }

                if (_security.Register(security))
                {
                    FormsAuthentication.SetAuthCookie(model.FullName, false /* createPersistentCookie */);
                    return RedirectToAction("Index", "Home");
                }
                else
                {
                    ModelState.AddModelError("", "注册失败，请重试。");
                }
            }

            return View(model);
        }

    }
}
